package com.narunas.containers;

import java.util.ArrayList;

import com.narunas.R;
import com.narunas.animation.ContainerAnimationBuilder;
import com.narunas.shapes.ChildShapeBox;
import com.narunas.shapes.ChildShapeOval;
import com.narunas.shapes.ChildShapeRoundRec;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ContainerBuilder {

	public static final ViewGroup createScalingContainer(final Activity mAct) {
		
		LinearLayout lPanel = new LinearLayout(mAct);
		lPanel.setBackgroundColor(Color.LTGRAY);
		//lPanel.setBackgroundResource(R.drawable.gradient_01);
		lPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		lPanel.setOrientation(LinearLayout.VERTICAL);
		lPanel.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
		lPanel.setVerticalGravity(Gravity.CENTER_VERTICAL);
		/*
		LinearLayout aPanel = new LinearLayout(mAct);
	
		aPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		aPanel.setOrientation(LinearLayout.VERTICAL);
		aPanel.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
		aPanel.setVerticalGravity(Gravity.CENTER_VERTICAL);
		*/
		// >>
		//ChildShapeOval oval = new ChildShapeOval(mAct);
		
		
		LinearLayout bPanel = new LinearLayout(mAct);
		
		bPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		bPanel.setOrientation(LinearLayout.VERTICAL);
		bPanel.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);

		bPanel.setBackgroundColor(0x990011FF);
		bPanel.setPadding(20, 20, 20, 20);
		


		//ChildShapeRoundRec rRec = new ChildShapeRoundRec(mAct, "he he he ");
		//rRec.setPadding(10, 10, 10, 10);
		
		
		TextView txt = new TextView(mAct);
		txt.setId(100);
		
		txt.setBackgroundColor(0x00cdcdcd);
		txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ));
		
		txt.setPadding(5, 5, 5, 5);
		
		txt.setText("Child that will not scale");
		txt.setTextColor(Color.WHITE);
		txt.setGravity(Gravity.CENTER);
		txt.setShadowLayer(2,2,2, 0x99111111);
		txt.setDrawingCacheQuality(1);
		//txt.setBackgroundColor(new RadialGradient(33, 33, 120, 0xFFededed, 0xFF636363, Shader.TileMode.MIRROR));
		txt.setWidth(150);
		txt.setHeight(150);
		//txt.forceLayout();
		
		// this is an issue - the generic view imported seems to disable any other children added after it
		// probably lacking correct configuration
		bPanel.addView(txt);
		/*
		CheckBox chk = new CheckBox(mAct);
		bPanel.addView(chk);
		
		bPanel.addView(rRec);
		
		*/
		
		//ChildShapeBox box = new ChildShapeBox(mAct);
		Button btn = new Button(mAct);
		btn.setWidth(150);
		btn.setHeight(30);
		btn.setText("immovable");
		
		//Button btn1 = new Button(mAct);
		//TextView txt = new TextView(mAct);

		//
		// the order of children is set here
		//
		//
		lPanel.addView(btn);
		
		//lPanel.addView(aPanel);
		lPanel.addView(bPanel);
		//lPanel.addView(oval);	
		
		
		AnimationSet alphaSet = new AnimationSet(true);
			
		Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
		alphaAnimation.setDuration(800);
		alphaSet.addAnimation(alphaAnimation);
		
		LayoutAnimationController alphaController = new LayoutAnimationController(alphaSet, 0.25f);
		//aPanel.setLayoutAnimation(alphaController);
	  
		
		 AnimationSet set = new AnimationSet(true);
		  
		 Animation animation = new TranslateAnimation(
		      
			Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
		    Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
		 );
		 
		
		 animation.setDuration(500);
		 set.addAnimation(animation);

		  LayoutAnimationController controller =
		      new LayoutAnimationController(set, 0.25f);
		
		  lPanel.setLayoutAnimation(controller);
		
		  //
		  //
		  //
		  /*
		  AnimationSet setB = new AnimationSet(true);
		  
		  Animation animationB = new AlphaAnimation(
				  
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
			    Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
			 );
			 
			
			 animationB.setDuration(1000);
			 setB.addAnimation(animation);

			  LayoutAnimationController controllerB =
			      new LayoutAnimationController(set, 0.25f);
			
			  bPanel.setLayoutAnimation(controllerB);
		  
			*/
		  
		  
		return lPanel;
		
	}
	
	public static final ViewGroup createDialerContainer(Activity mAct) {
		
		LinearLayout mainPanel = new LinearLayout(mAct);
		mainPanel.setId(1);
		
		//mainPanel.setBackgroundColor(Color.LTGRAY);
		//mainPanel.setBackgroundColor(Color.BLUE);
		mainPanel.setBackgroundResource(R.drawable.gradient_01);
		mainPanel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mainPanel.setPadding(1, 20, 1, 1);
		
		mainPanel.setOrientation(LinearLayout.VERTICAL);
		mainPanel.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
		mainPanel.setVerticalGravity(Gravity.CENTER_VERTICAL);
		
		mainPanel.setAlwaysDrawnWithCacheEnabled(true);
		
		
		LinearLayout dPanel = new LinearLayout(mAct);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		dPanel.setOrientation(LinearLayout.HORIZONTAL);
		dPanel.setPadding(0, 0, 0, 0);
		dPanel.setLayoutParams(params);
	
			
			
	
		//dPanel.setBackgroundResource(R.drawable.gradient_01);
		
		//dPanel.setBackgroundColor(Color.RED);
		//dPanel.setFadingEdgeLength(30);
		dPanel.setId(2);
		
		Button vButton = new Button(mAct);
		vButton.setId(10);
		vButton.setHeight(80);
		vButton.setWidth(10);
		//vButton.setOnClickListener(handleOpenCloseClick);
		
		RelativeLayout cPanel = new RelativeLayout(mAct);
		LayoutParams cParams  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		cPanel.setLayoutParams(cParams);
		cPanel.setPadding(40, 10, 10, 10);
		cPanel.setBackgroundColor(Color.MAGENTA);
		
		dPanel.addView(vButton);
		dPanel.addView(cPanel);
	
		//dPanel.layout(400, 0, 0, 0);
		
		//dPanel.setBackgroundColor(com.narunas.R.drawable.gradient_01);
		/*
		 LayoutAnimationController controller =
		      new LayoutAnimationController(ContainerAnimationBuilder.createTranslateLayoutAnimation(true), 0.95f);
		
		//dPanel.setLayoutAnimation(controller); 
		mainPanel.setLayoutAnimation(controller);
		*/
		ChildShapeRoundRec rect = new ChildShapeRoundRec(mAct, "HOhoho");
		
		//rect.setBackgroundColor(Color.GREEN);
		rect.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		
		//dPanel.addView(rect);
		
		//Button btn = new Button(mAct);
		//mainPanel.addView(btn);
		mainPanel.addView(dPanel);
		//mainPanel.layout(200, 0, 0, 0);
		//Toast.makeText(this, "Children " + mainPanel.getChildCount(),Toast.LENGTH_SHORT);
		return mainPanel;	
		
		  
	}
	
	public static final ViewGroup createProcButtonsContainer(Activity mAct) {
		
		ArrayList children = new ArrayList();
		ArrayList fChildren = new ArrayList();
		
	
		
		
		LinearLayout mLayout = new LinearLayout(mAct);
		
		mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		mLayout.setOrientation(LinearLayout.VERTICAL);
		mLayout.setBackgroundColor(Color.BLUE);
		//mLayout.setGravity(LinearLayout.VERTICAL);
		
		//mLayout.setVerticalGravity(LinearLayout.VERTICAL);
		
		//mLayout.addFocusables(children, 1);
		
		for(int i = 0; i < 5; i++){
			
			Button btn = new Button(mAct);
			btn.setVisibility(1);
			btn.setText("this is child");
				
			//children.add(btn);
			//ChildShapeRoundRec box = new (mAct, " child # "  + i);
			//box.setText("child # :" + i);
			TextView ttx = new TextView(mAct);
			ttx.setText("this is child # : " + i);
			ttx.setBackgroundColor(Color.WHITE);
			
			ChildShapeRoundRec vv = new ChildShapeRoundRec(mAct, "nnn");
			ArrayList vvl = new ArrayList();
			vvl.add(vv);
			ttx.addTouchables(vvl);
			
			
			
			
			children.add(ttx);
			
			mLayout.addView((View)children.get(i) );
			
			//btn.setText("this is child # : " + i);
		}
		/*
		LinearLayout fl = new LinearLayout(mAct);
		fl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		fl.setBackgroundColor(Color.WHITE);
		fl.addView(new ChildShapeRoundRec(mAct));
		fChildren.add(fl);
		*/
		
		mLayout.addFocusables(fChildren, 1);
		
		//mLayout.addView((View)children.get(0) );
		//btn.setText("this is child # : " + mLayout.getChildCoun;
		
		
		return mLayout;
	}
	
	
	private static Animation getDialPadEntryAnimation() {
		
		Animation animation = new TranslateAnimation(	      
				Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
		 );
		animation.setDuration(1000);
		return animation;
	}
	
	
}
